<template>
  <div class="build-code">
<!--    <TableForm :config="tableConfig"  ref="v2Table"></TableForm>-->

    <el-collapse accordion @change="focusCodeEdit">
      <el-collapse-item v-for="(value ,key) in toCode(dMap)" :name="key" >
        <template slot="title">{{key}}</template>
        <CodeEdit :value="value" :read-only="true" :ref="key"></CodeEdit>
      </el-collapse-item>
    </el-collapse>
  </div>
</template>

<script>
import Service from "api/Service";
import http from "network/http";
import util from "utils/util";

export default {
  name: "BuildCode",
  props:{
    tableName:{
      type: Array,
      default(){
        return []
      }
    },
    dataBaseName: "" ,
    tabsValue: "",
    dsId: null ,


  },
  data(){
    return {
      api:{
        list: `/${Service.itToolService}/toolDs/queryColumnList`
      },
      dMap: {} ,
      tableColumns: [],
      useCase: true
    }
  },
  methods:{
    focusCodeEdit(key,v){
      if(key){
        this.$refs[key][0].refresh()
      }

    },
      queryList(){
        http.post(this.api.list ,this.params).then(res=>{
          let data = res.data ;
          let dMap = {} ;
          data.forEach(item=>{
            if(!dMap[item.tableName]){
              dMap[item.tableName] = []
            }
            dMap[item.tableName].push(item)
          })
          this.dMap = dMap ;
          this.tableColumns = data
        })
      },
    toCode(dMap){
        let str = `/*comment*/\n private Type Name ; \n`
        let res = {} ;
        for(let k in dMap){
          let values = dMap[k] ;
          res[k] = values.map(f=>{
            return str.replace("comment",f.columnComment)
                .replace("Type",f.javaType )
                .replace("Name",util.ifTruthy(this.useCase,f.fieldName , f.columnName))
          }).join("")
        }
        return res ;
    }
  },
  computed:{
    params(){
      return {
        dataBaseName: this.dataBaseName ,
        tableName: this.tableName.join(",") ,
        dsId: this.dsId
      }
    }
  },
  watch:{
    tableName(){
      if(this.tabsValue === "code"){
        this.$nextTick(()=>{
          this.queryList();
        })
      }
    }
  }
}
</script>

<style scoped>

</style>